package com.alan.wifishare.presenter;

import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.text.TextUtils;

import com.alan.library.allog.KLog;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.model.business.ScanedWifiInfo;
import com.alan.wifishare.presenter.wifi.ALNetwork;
import com.alan.wifishare.presenter.wifi.NetworkStateListener;
import com.alan.wifishare.presenter.wifi.NetworkStateObserverManager;
import com.alan.wifishare.utils.WifiUtil;
import com.alan.wifishare.view.iview.IVCNTConnectLink;

import java.util.ArrayList;
import java.util.List;

/**
 * @className: CNTConnectLinkPS
 * @describtion: CNTConnectLinkPS
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/26 17:38
 */
public class CNTConnectLinkPS {

    private IVCNTConnectLink ivcntConnectLink;
    private NetworkStateListener networkStateListener;

    public CNTConnectLinkPS(IVCNTConnectLink ivcntConnectLink) {
        this.ivcntConnectLink = ivcntConnectLink;
    }

    /**
     * 扫描wifi
     */
    public void scanWifi(){
        // 扫描周围的wifi
        GlobalField.wifiUtil.openWifi();
        GlobalField.wifiUtil.startScan();
        List<ScanedWifiInfo> scanedWifiInfoList = new ArrayList<>();
        List<ScanResult> wifiScanResultList = GlobalField.wifiUtil.getWifiList();
        for (ScanResult scanResult:wifiScanResultList) {
            ScanedWifiInfo scanedWifi = new ScanedWifiInfo();
            scanedWifi.setSSID(scanResult.SSID);
            scanedWifi.setMac(scanResult.BSSID);
            scanedWifi.setShared(false);
            scanedWifiInfoList.add(scanedWifi);
        }
        ivcntConnectLink.showScanedWifiList(scanedWifiInfoList);
    }

    /**
     * 点击连接按钮时执行连接Link的操作
     */
    public void connectLink() {
        final String ssid = ivcntConnectLink.getLinkSSID();
        String password = ivcntConnectLink.getLinkPassword();
        KLog.d("ssid = " + ssid);
        KLog.d("password = " + password);
        if(ssid == null || TextUtils.isEmpty(ssid)){
            ivcntConnectLink.toastMsg("SSID不能为空");
            return;
        }
        if(password == null || TextUtils.isEmpty(password)){
            ivcntConnectLink.toastMsg("密码不能为空");
            return;
        }else if(password.length() < 8){
            ivcntConnectLink.toastMsg("密码不能少于8位");
            return;
        }
        ivcntConnectLink.showProgress();
        if(networkStateListener == null){
            networkStateListener = new NetworkStateListener() {
                @Override
                public void onStateChanged(int type, int state) {
                    KLog.d("type = " + type + "######state = " + state);
                    if(type == ALNetwork.Type.CONNECTIVITY && state == ALNetwork.State.CONNECTIVITY_CONNECTED){
                        KLog.d("getWifiInfo = " + GlobalField.wifiUtil.getWifiInfo().toString());
                        WifiInfo wifiInfo = GlobalField.wifiUtil.getWifiInfo();
                        KLog.d("wifiInfo = " + wifiInfo.toString());
                        String getWifiSSID = wifiInfo.getSSID();
                        getWifiSSID = getWifiSSID.substring(1, getWifiSSID.length()-1);
                        ivcntConnectLink.hideProgress();
                        if(getWifiSSID.equals(ssid)){
                            ivcntConnectLink.connectLinkSuccess();
                        }
                    }
                }
            };
            NetworkStateObserverManager.getInstance().registerObserver(networkStateListener);
        }
        //根据SSID和密码自动连接wifi，并设置wifi状态监听
        if(!GlobalField.wifiUtil.isWifiEnabled()){
            GlobalField.wifiUtil.openWifi();
        }
        GlobalField.wifiUtil.connectToWifi(ssid, password, WifiUtil.TYPE_WPA);
    }
}
